#! /usr/bin/env python
#coding=utf-8

"""
    2011 Boris Tatarintsev
    
    Interface classes and definitions
    Interface is based on the PGU library which can be downloaded
    at http://code.google.com/p/pgu/
"""

from pgu import gui

import ljnetworkapi
import threading
import ljfc

scanThr = None

class Open(gui.Button):    
    def __init__(self, **params):
        params['value'] = 'Open data file'
        gui.Button.__init__(self, **params)        
        self.connect(gui.CLICK, open_file_browser, None)        
        
class Scan(gui.Button):
    def __init__(self, **params):
        params['value'] = 'Scan new user'
        gui.Button.__init__(self, **params)  
        self.connect(gui.CLICK, UserNameDialog().open, None)

class ProgressDialog(gui.Dialog):
    
    def __init__(self, **params):
        self.m_userName = params['user']
        self.m_percentLabel = gui.Label(self.m_userName + ' 0%')
        title = gui.Label("Scan progress")        
        main = gui.Table()
        main.tr()        
        main.td(self.m_percentLabel)
        """
        main.tr()
        e = gui.Button("Cancel")
        e.connect(gui.CLICK, self.stopScan, None)
        main.td(e)
        """
        gui.Dialog.__init__(self, title, main)    
        
    def updateProgress(self, stat):
        if stat.m_percent != 100:
            self.m_percentLabel.set_text(self.m_userName + ' ' + str(stat.m_percent) + '%')
        else:
            self.m_percentLabel.set_text("Done!")
        
    def stopScan(self, obj):
        global scanThr
        pass

class UserNameDialog(gui.Dialog):
    
    def __init__(self, **params):        
        title = gui.Label("Enter user name to scan")
        
        main = gui.Table()
        
        main.tr()
        self.m_inputUser = gui.Input(name = "ljuser", value = '', size = 15)
        main.td(self.m_inputUser)
        
        e = gui.Button("check")
        e.connect(gui.CLICK, check_lj_user, self.m_inputUser.value)
        main.td(e)        
            
        main.tr()
        main.td(gui.Spacer(width = 0, height = 4))
        
        main.tr()
        main.td(gui.Label("scan depth"))
        self.m_inputDepth = gui.Input(name = "depth", value = '2', size = 3)
        main.td(self.m_inputDepth)
        
        main.tr()
        main.td(gui.Spacer(width = 0, height = 18))
        main.tr()
        self.m_skipCommSwitch = gui.Switch(value = True, name = 'skipCommunities')
        main.td(self.m_skipCommSwitch)
        main.td(gui.Label("skip communities"))
        
        main.tr()        
        main.td(gui.Spacer(width = 0, height = 2))
        main.tr()
        self.m_collectInterestsSwitch = gui.Switch(value = False, name = 'collectIterests')
        main.td(self.m_collectInterestsSwitch)
        main.td(gui.Label("collect interests"))
        
        main.tr()
        main.td(gui.Spacer(width = 0, height = 2))
        main.tr()
        self.m_mutualFriendsOnly = gui.Switch(value = False, name = 'mutualOnly')
        main.td(self.m_mutualFriendsOnly)
        main.td(gui.Label("scan mutual friends only"))        
        
        main.tr()
        main.td(gui.Spacer(width = 0, height = 2))
        main.tr()
        self.m_verboseModeSwitch = gui.Switch(value = False, name = 'verboseMode')
        main.td(self.m_verboseModeSwitch)
        main.td(gui.Label("verbose mode"))
                
        main.tr()
        main.td(gui.Spacer(width = 0, height = 10))        
        main.tr()
        e = gui.Button("Okay")
        e.connect(gui.CLICK, self.startScan, self)
        main.td(e)
        e = gui.Button("Cancel")
        e.connect(gui.CLICK, self.close, None)
        main.td(e)
        ##        
        gui.Dialog.__init__(self, title, main)
        
    def startScan(self, obj):
        global scanThr
        if (scanThr != None and not scanThr.isAlive()) or scanThr == None:

            # init thread
            me = obj
                        
            scanThr = ljfc.MyThread("id1")            
            pdialog = ProgressDialog(user = self.m_inputUser.value)
            scanThr.setParams( ljfc.scanUser, *(self.m_inputUser.value, self.m_inputDepth.value, self.m_skipCommSwitch.value, self.m_collectInterestsSwitch.value, \
                                self.m_mutualFriendsOnly.value, self.m_verboseModeSwitch.value, pdialog.updateProgress) )
            
            me.close()
            # open progress dialog
            pdialog.open()
            # start scan
            scanThr.start()
                    
def open_file_browser(arg):
    d = gui.FileDialog()
    d.connect(gui.CHANGE, handle_file_browser_closed, d)
    d.open()
    
def handle_file_browser_closed(dlg):
    if dlg.value:
        ljfc.drawGraph(dlg.value)

def check_lj_user(arg):
    print ljnetworkapi.LJUserSession.checkUserExists(arg)

def quit_app(*args):    
    app = gui.Desktop()
    app.quit()
    exit(0)

def createUI():
    app = gui.Desktop()    
    app.connect(gui.QUIT, quit_app, None)

    c = gui.Table(width = 400, height = 300)

    c.tr()
    e = Scan()
    c.td(e)
    e = Open()
    c.tr()
    c.td(e)
    ##
    app.run(c)
    ##

createUI()
